package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-08-25
 * Time:0:38
 */
//多态
//优缺点

public class Test {
    public static void main(String[] args) {
        //使用多态
        Cycle cycle = new Cycle();
        Rect rect = new Rect();
        Flower flower = new Flower();
        Triangle triangle = new Triangle();//多态可扩展性更强，使用多态的方式代码改动成本也比较低

        //创建了一个 Shape 对象的数组
        Shape[] shapes = {cycle,rect,flower,rect,flower,triangle};
        for (Shape shape : shapes){
            shape.draw();
        }

        /*for (int i = 0; i < shapes.length; i++) {
            *//*Shape shape = shapes[i];
            shape.draw();*//*
            shapes[i].draw();
        }*/
    }
    public static void main2(String[] args) {
        //不基于多态，使用if-else实现多个图形打印
        Cycle cycle = new Cycle();
        Rect rect = new Rect();
        Flower flower = new Flower();

        String[] Shape = {"cycle","rect","cycle","flower","rect"};
        for (String s : Shape){
            if (s.equals("cycle")){
                cycle.draw();
            } else if (s.equals("rect")) {
                rect.draw();
            }else {
                flower.draw();
            }
        }
    }
    public static void draw(Shape shape){
        shape.draw();
    }
    public static void main1(String[] args) {
        Cycle cycle = new Cycle();
        Rect rect = new Rect();
        Flower flower = new Flower();

        draw(cycle);
        draw(rect);
        draw(flower);
    }
}
